//
//  QNContentUploadManager.swift
//  TestDemo
//
//  Created by ayin on 2019/2/12.
//  Copyright © 2019 aYin. All rights reserved.
//

import Foundation

class QNContentUploadManager:NSObject {
    fileprivate var manager: QNUploadManager!
    fileprivate var option : QNUploadOption!
    fileprivate var progressBlock : ((Float)->Void)?
    
    static let share = QNContentUploadManager()
    
    override init() {
        super.init()
        
        var file:QNFileRecorder? = nil
        do {
            // 没有错误消息抛出
            file = try QNFileRecorder.init(folder: NSTemporaryDirectory() + "qiniutest")
        } catch {
            // 有一个错误消息抛出
            
        }
        let config = QNConfiguration.build { (builder) in
            if let b = builder {
                b.setZone(QNFixedZone.zone0())
                b.chunkSize = 256 * 1024
                if let aFile = file {
                    b.recorder = aFile
                }
            }
        }
        self.manager = QNUploadManager.sharedInstance(with: config)
        
        
        
        
        self.option = QNUploadOption.init(mime: nil, progressHandler: { [weak self] (key, percent) in
            guard let weakSelf = self else {return}
            if weakSelf.progressBlock != nil {
                weakSelf.progressBlock!(percent)
            }
        }, params: nil, checkCrc: false, cancellationSignal: { () -> Bool in
            return false
        })
    }
    
    public func uploadVideo(filePath: String, token: String = "92SPonlDvYbu91VZOxvrHUmc9pHi3B8Wy5PUlzQ8:6-RPdTyRKIByrMBjHV8MIv-wJa4=:eyJzY29wZSI6ImNvbW11bml0eSIsInJldHVybkJvZHkiOiJ7XCJrZXlcIjpcIiQoa2V5KVwiLFwiaGFzaFwiOlwiJChldGFnKVwiLFwiYnVja2V0XCI6XCIkKGJ1Y2tldClcIixcImZzaXplXCI6JChmc2l6ZSl9IiwiZGVhZGxpbmUiOjE1NDk5NTkzMzl9", progressBlock: ((Float)->Void)?, responseBlock: @escaping (Bool, Bool, String?)->Void) {
        self.progressBlock = progressBlock
        self.manager.putFile(filePath, key: "testVideo", token: token, complete: { (info, key, resp) in
            if let responseInfo = info {
                if responseInfo.isOK {
                    responseBlock(true, false, key)
                }else {
                    if responseInfo.statusCode == 401 {
                        responseBlock(false, true, nil)
                    }else {
                        responseBlock(false, false, nil)
                    }
                }
            }else {
                responseBlock(false, false, nil)
            }
        }, option: self.option)
        
        
//        if let data = try? Data.init(contentsOf: videoUrl) {
//            self.manager.put(data, key: "testVideo", token: token, complete: { (info, text, resp) in
//                if let responseInfo = info {
//                    if responseInfo.isOK {
//                        responseBlock(true, false, text)
//                    }else {
//                        if responseInfo.statusCode == 401 {
//                            responseBlock(false, true, nil)
//                        }else {
//                            responseBlock(false, false, nil)
//                        }
//                    }
//                }else {
//                    responseBlock(false, false, nil)
//                }
//            }, option: self.option)
//        }
    }
}
